//
//  ByUIRealTimeAlertTypeSelectView.m
//  laserdevice_ios
//
//  Created by pro on 16/11/25.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByUIRealTimeAlertTypeSelectView.h"

@implementation ByUIRealTimeAlertTypeSelectView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupViews];
    }
    return self;
}


- (void)setupViews {
    
    NSArray *texts = @[STRING(@"zhendong"),
                       STRING(@"lingsheng"),
                       STRING(@"zhendong+lingsheng")];
    
    CGFloat x, y, width, height;
    width = self.width / texts.count;
    height = self.height;
    for (int i = 0; i < texts.count; i ++) {
        
        x = i * width;
        y = 0;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(x, y, width, height);
        button.titleLabel.font = FONT(14);
        [button setTitle:texts[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"real-time_weixuanzhong"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"real-time_xuanzhong"] forState:UIControlStateSelected];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 3, 0, -3)];
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, -3, 0, 3)];
        [button addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        button.tag = i;
        
        if (i == 0) {
            button.selected = YES;
        }
    }
}

- (void)tapAction:(UIButton *)sender {
    
    if (self.selectedAction) {
        self.selectedAction(sender.tag);
    }
    
    // 点击已经选中的，就不做操作
    if (sender.selected) {
        return;
    }
    
    
    for (UIButton *button in self.subviews) {
        button.selected = NO;
    }
    sender.selected = YES;

}

@end
